package com.sendi.system.action;
 
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
import org.hibernate.Query;
import org.hibernate.Session;
import com.sendi.system.entity.SysHostTree;
import com.sendi.system.util.tree.ConTreeData;
/*
 * 设备树管理  SysHostTree
 */
public class HostTreeAction extends SysAction {
    private Integer  id;
    	
   private String deleterecord;
   private int  clickleafid;
   public String reloadflag="no";
 
   private Integer parentid;
   private String nodeName;
   private String hostname;
   private Integer hostId;
   private String ishost="n";
   private String hosttype;	   
	public HostTreeAction()
	{
		super();
		data_session_name="DepartmentActionListData";
	}
	
	/*
	 * 列出设备树结构
	 */
	public void listhostttree()
	{
	   
 		String hql="from SysHostTree where  parentid="+this.Treeid;
 	 
		List datas=null;
	 	datas=serviceSupportImp.findByHQL(hql);
	    ArrayList<ConTreeData> children=new ArrayList<ConTreeData>();
	    
		for(Object o:datas)
		{
			ConTreeData ct=new ConTreeData();
			SysHostTree f=(SysHostTree)o;
		 	ct.setId(f.getId());
		   	ct.setText(f.getNodeName());
		  	if(f.getIshost().equals("y"))
		  	{
		  		ct.setLeaf(true);
		  	}
		    children.add(ct);
	 	}
		   String jsons = JSONArray.fromObject(children).toString();
 	       writeResponseText(jsons);
	   
	}
	 
	public void hosttreeInit()
	{
		 
		 List datas=null;
	 
		if(this.reloadflag.equalsIgnoreCase("no")&&getSession().getAttribute(data_session_name)!=null)
		{
			datas=(List)getSession().getAttribute(data_session_name);
	 	}
		else
		{
			addDataToSession();
			datas=(List)getSession().getAttribute(data_session_name);
		}
	 
	 	writeJSONArray(datas);
		 
	}
	
	public void addDataToSession()
	{
		List datas=null;
		String hql="from  SysHostTree where parentid="+this.clickleafid;
	  	datas=serviceSupportImp.findByHQL(hql);
		getSession().setAttribute(data_session_name,datas);
 	}
	
 
	public void queryhosttreedata()
	{
		   try
		   {
			     Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
			 	StringBuffer hql=new StringBuffer("from SysHostTree  where 1=1 ");
				if(nodeName!=null&&nodeName.trim().length()>0)
				{
					hql.append(" and  nodeName like :nodeName ");
				}
			   Query query= session.createQuery(hql.toString());
				   if(nodeName!=null&&nodeName.trim().length()>0)
					{
					  query.setParameter("nodeName", "%"+nodeName+"%");
					}
				    List datas=query.list();
				   getSession().setAttribute(data_session_name,datas);	
				    writeResponseText("{success:true,msg:'success',datasize:"+datas.size()+"}");
		   			}
		   catch (Exception e) {
			   writeResponseText("{success:false,msg:'failure'}");
				 
		}
		    
		
	}

 
	 public void addHostTreedata()
	 {
		 
	 	 
		 SysHostTree sysHostTree=new SysHostTree();
		 sysHostTree.setIshost(ishost);
		 sysHostTree.setNodeName(nodeName);
	  	 sysHostTree.setParentid(parentid);
	  
	  	
		 if(this.ishost.equals("y"))
		 {
			 sysHostTree.setHostId(hostId);
			 sysHostTree.setHostname(this.nodeName);
		     sysHostTree.setHosttype(hosttype);
		 }
		 else
		 {
			 sysHostTree.setHostId(null);
			 sysHostTree.setHostname(null);
		 }
		
		
		 
	   	try {
				serviceSupportImp.save(sysHostTree);
			 	getSession().removeAttribute(data_session_name);
				addDataToSession();
				writeResponseText("{success:true,msg:'success'}");
		 	} catch (Exception e) {
				writeResponseText("{success:false,msg:'faild',errors:{"+e.getMessage()+"}}");
			  e.printStackTrace();
			}
	 }

	 
	 public void excutModifyQueryhosttree()
		{
		 
			String hql="from SysHostTree where 1=1 and id="+this.id; 
		 
		 	List datas=serviceSupportImp.findByHQL(hql);
		  
		 	SysHostTree sysHostTree=(SysHostTree)datas.get(0);
			try {
			 	writeResponseText("{success:true,datas:"+JSONObject.fromObject(sysHostTree).toString()+"}");
			} catch (Exception e) {
		 	   writeResponseText("{success:false,msg:'faild',errors:{"+e.getMessage()+"}}");
			 	e.printStackTrace();
			}
			
		}
		public void excuteModifyhosttree()
		{
		 
			String hql="from SysHostTree where 1=1 and id="+this.id; 
			
			List datas=serviceSupportImp.findByHQL(hql);
			SysHostTree sysHostTree=(SysHostTree)datas.get(0);
			 sysHostTree.setIshost(ishost);
			 sysHostTree.setNodeName(nodeName);
		  	 sysHostTree.setParentid(parentid);
		  
		  	
			 if(this.ishost.equals("y"))
			 {
				 sysHostTree.setHostId(hostId);
				 sysHostTree.setHostname(this.nodeName);
			     sysHostTree.setHosttype(hosttype);
			    
			 }
			 else
			 {
				 sysHostTree.setHostId(null);
				 sysHostTree.setHostname(null);
			 }
			
			 
		 	serviceSupportImp.update(sysHostTree);
			
			getSession().removeAttribute(data_session_name);
		    addDataToSession();
			writeResponseText("{success:true,msg:'success'}"); 
		}
		
		public void excuteDeletehosttree()
		{
		 //todo 这里还差一个递归算法进行删除，目前只删除到下一级的节点，再下一级的节点不能删除了
		  String hql="delete SysHostTree where id in ("+deleterecord+") or  parentid in ("+deleterecord+")";
		  Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
		  session.beginTransaction();
		  session.createQuery(hql).executeUpdate();
		  session.getTransaction().commit();
		  addDataToSession();
		  writeResponseText("{success:true,msg:'success'}"); 
		}
 
		
	public Integer getId() {
		return id;
	}



	public void setId(Integer id) {
		this.id = id;
	}
 
	public String getDeleterecord() {
		return deleterecord;
	}


	public void setDeleterecord(String deleterecord) {
		this.deleterecord = deleterecord;
	}

	public int getClickleafid() {
		return clickleafid;
	}

	public void setClickleafid(int clickleafid) {
		this.clickleafid = clickleafid;
	}

	public String getReloadflag() {
		return reloadflag;
	}

	public void setReloadflag(String reloadflag) {
		this.reloadflag = reloadflag;
	}

 

	public Integer getParentid() {
		return parentid;
	}

	public void setParentid(Integer parentid) {
		this.parentid = parentid;
	}

 

	public String getHosttype() {
		return hosttype;
	}

	public void setHosttype(String hosttype) {
		this.hosttype = hosttype;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public Integer getHostId() {
		return hostId;
	}

	public void setHostId(Integer hostId) {
		this.hostId = hostId;
	}

	public String getIshost() {
		return ishost;
	}

	public void setIshost(String ishost) {
		this.ishost = ishost;
	}

	 
	 
}
